package com.project.erp.dao.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.project.erp.common.dao.HibernateDao;
import com.project.erp.common.utils.Page;
import com.project.erp.model.system.Role;

@Repository
public class RoleDao extends HibernateDao<Role, Long>{
	
	public Page<Role> getRolePageBySearch(Page<Role> page,Role role){
		if(null == role){
			page = this.getAll(page);
		}
		List<Criterion> list = new ArrayList<Criterion>();
		if(StringUtils.isNotEmpty(role.getName())){
			Criterion name = Restrictions.ilike("name",role.getName(),MatchMode.ANYWHERE);
			list.add(name);
		}
		if(StringUtils.isNotEmpty(role.getDescription())){
			Criterion loginName = Restrictions.ilike("description",role.getDescription(),MatchMode.ANYWHERE);
			list.add(loginName);
		}
		page = this.findPage(page,list.toArray(new Criterion[list.size()]));
		//设置权限码为空
		for (Role r : page.getResult()) {
			r.setPrivileges(null);
		}
		return page;
	}
}
